package com.bjfu.gmms.servlet.departmentHeadServlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.PrimaryDisciplineInfoMapper;
import com.bjfu.gmms.pojo.PrimaryDisciplineInfo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/PrimaryDisciplineOperateServlet")
public class PrimaryDisciplineOperateServlet extends HttpServlet {
    private SqlSession sqlSession = null;

    // 获取数据库连接
    private SqlSession getSqlSession() {
        if (sqlSession == null) {
            sqlSession = MyBatisUtil.getSqlSession("development");
        }
        return sqlSession;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        try (SqlSession sqlSession = getSqlSession()) {
            PrimaryDisciplineInfoMapper mapper = sqlSession.getMapper(PrimaryDisciplineInfoMapper.class);

            if ("add".equals(action)) {
                // 处理添加新一级学科的请求
                request.setCharacterEncoding("UTF-8");
                String primaryDisciplineId = request.getParameter("primaryDisciplineId");
                String name = request.getParameter("name");
                HttpSession session = request.getSession();
                int i_collegeId = Integer.parseInt(session.getAttribute("collegeId").toString());
                String collegeId = String.format("%03d", i_collegeId);


                PrimaryDisciplineInfo primaryDisciplineInfo = new PrimaryDisciplineInfo();
                primaryDisciplineInfo.setPrimaryDisciplineId(primaryDisciplineId);
                primaryDisciplineInfo.setName(name);
                primaryDisciplineInfo.setCollegeId(collegeId);

                // 插入数据库
                mapper.insertPrimaryDisciplineInfo(primaryDisciplineInfo);
                sqlSession.commit(); // 提交事务

                response.sendRedirect("/WEB-INF/department_head_view/editPrimaryDiscipline.jsp"); // 添加后重定向到列表页面
            } else if ("edit".equals(action)) {
                // 处理编辑一级学科的请求
                String id = request.getParameter("id");
                String name = request.getParameter("name");

                PrimaryDisciplineInfo primaryDisciplineInfo = new PrimaryDisciplineInfo();
                primaryDisciplineInfo.setPrimaryDisciplineId(id);
                primaryDisciplineInfo.setName(name);

                // 更新数据库
                mapper.updatePrimaryDisciplineInfo(primaryDisciplineInfo);
                sqlSession.commit(); // 提交事务

                response.sendRedirect("primaryDisciplineList.jsp"); // 编辑后重定向到列表页面
            } else if ("delete".equals(action)) {
                // 处理删除一级学科的请求
                String id = request.getParameter("id");

                // 删除数据库
                mapper.deletePrimaryDisciplineInfo(id);
                sqlSession.commit(); // 提交事务

                response.sendRedirect("primaryDisciplineList.jsp"); // 删除后重定向到列表页面
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 错误处理
        }
    }
}